package com.speljohan.rsbot.script;

import java.util.HashSet;
import java.util.Set;

import javax.swing.JOptionPane;

public class DepreciatedWarner {
	private static Set<Class<?>> warned = new HashSet<Class<?>>();

	public static Class<?> getCaller(final int i) { // 0 is the caller. 1 is
		// their caller etc...
		class SM extends SecurityManager {
			@Override
			public Class<?>[] getClassContext() {
				return super.getClassContext();
			}
		}
		return new SM().getClassContext()[2 + i];
	}

	public static void warn(final Class<?> clazz) {
		if (DepreciatedWarner.warned.contains(clazz)) {
			return;
		}
		DepreciatedWarner.warned.add(clazz);
		JOptionPane
		.showConfirmDialog(
				null,
				"The class "
				+ clazz.getName()
				+ " is using depreciated methods.\n"
				+ "Please inform the script writer that they should use up to date API.\n"
				+ "Script writers should not be using depreciated API or your script\n"
				+ "will break in the near future.", clazz
				.getName()
				+ " Depreciated", JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.ERROR_MESSAGE);
	}

	private DepreciatedWarner() {
	}
}
